package cn.stylefeng.guns.modular.nameNumebr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.nameNumebr.entity.NameNumber;
import cn.stylefeng.guns.modular.nameNumebr.enums.NameNumberExceptionEnum;
import cn.stylefeng.guns.modular.nameNumebr.mapper.NameNumberMapper;
import cn.stylefeng.guns.modular.nameNumebr.pojo.request.NameNumberRequest;
import cn.stylefeng.guns.modular.nameNumebr.service.NameNumberService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现层
 *
 * @author jizhongbao
 * @date 2022/03/08 14:51
 */
@Service
public class NameNumberServiceImpl extends ServiceImpl<NameNumberMapper, NameNumber> implements NameNumberService {

	@Override
    public void add(NameNumberRequest nameNumberRequest) {
        NameNumber nameNumber = new NameNumber();
        BeanUtil.copyProperties(nameNumberRequest, nameNumber);
        this.save(nameNumber);
    }

    @Override
    public void del(NameNumberRequest nameNumberRequest) {
        NameNumber nameNumber = this.queryNameNumber(nameNumberRequest);
        this.removeById(nameNumber.getId());
    }

    @Override
    public void edit(NameNumberRequest nameNumberRequest) {
        NameNumber nameNumber = this.queryNameNumber(nameNumberRequest);
        BeanUtil.copyProperties(nameNumberRequest, nameNumber);
        this.updateById(nameNumber);
    }

    @Override
    public NameNumber detail(NameNumberRequest nameNumberRequest) {
        return this.queryNameNumber(nameNumberRequest);
    }

    @Override
    public PageResult<NameNumber> findPage(NameNumberRequest nameNumberRequest) {
        LambdaQueryWrapper<NameNumber> wrapper = createWrapper(nameNumberRequest);
        Page<NameNumber> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<NameNumber> findList(NameNumberRequest nameNumberRequest) {
        LambdaQueryWrapper<NameNumber> wrapper = this.createWrapper(nameNumberRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author jizhongbao
     * @date 2022/03/08 14:51
     */
    private NameNumber queryNameNumber(NameNumberRequest nameNumberRequest) {
        NameNumber nameNumber = this.getById(nameNumberRequest.getId());
        if (ObjectUtil.isEmpty(nameNumber)) {
            throw new ServiceException(NameNumberExceptionEnum.NAMENUMBER_NOT_EXISTED);
        }
        return nameNumber;
    }

    /**
     * 创建查询wrapper
     *
     * @author jizhongbao
     * @date 2022/03/08 14:51
     */
    private LambdaQueryWrapper<NameNumber> createWrapper(NameNumberRequest nameNumberRequest) {
        LambdaQueryWrapper<NameNumber> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = nameNumberRequest.getId();
        String name = nameNumberRequest.getName();
        Integer number = nameNumberRequest.getNumber();

        queryWrapper.eq(ObjectUtil.isNotNull(id), NameNumber::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), NameNumber::getName, name);
        queryWrapper.eq(ObjectUtil.isNotNull(number), NameNumber::getNumber, number);

        return queryWrapper;
    }

}